package com.lxf.test.androidtest.abc;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.lxf.test.androidtest.R;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/2/23.
 */
public class MapActivity extends AppCompatActivity {
    private TextView accelerometerView;
    private TextView orientationView;
    private SensorManager sensorManager;
    private MySensorEventListener sensorEventListener;
    private ImageButton backbtn;
    private ImageView person;
    private int height ;    //屏幕高度
    private int witdh;  //屏幕宽度

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        height = metric.heightPixels;
        witdh = metric.widthPixels;
        person = (ImageView)findViewById(R.id.person);
        System.out.println("left:" + person.getLeft());
        System.out.println("totop:" + person.getTop());
        sensorEventListener = new MySensorEventListener();
        accelerometerView = (TextView) this.findViewById(R.id.accelerometerView);
        orientationView = (TextView) this.findViewById(R.id.orientationView);
        //获取感应器管理器
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

        backbtn = (ImageButton) findViewById(R.id.back);
        backbtn.setOnClickListener(new BackListener());
    }

    @Override
    protected void onResume()
    {
        //获取方向传感器
        Sensor orientationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        sensorManager.registerListener(sensorEventListener, orientationSensor, SensorManager.SENSOR_DELAY_NORMAL);

        //获取加速度传感器
        Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(sensorEventListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
        super.onResume();
    }

    private final class MySensorEventListener implements SensorEventListener
    {
        //可以得到传感器实时测量出来的变化值
        @Override
        public void onSensorChanged(SensorEvent event)
        {
            //得到方向的值
            if(event.sensor.getType()==Sensor.TYPE_ORIENTATION)
            {
                int a,b;
                float x = event.values[SensorManager.DATA_X];
                float y = event.values[SensorManager.DATA_Y];
                float z = event.values[SensorManager.DATA_Z];
                orientationView.setText("Orientation: " + x + ", " + y + ", " + z);
//                person.setPivotX(x);
//                person.setPivotY(y);
                a = (int) x;
                b = (int) y + height/2;
                WidgetController c = new WidgetController();
                c.setLayout(person, a,b);

            }

            //得到加速度的值
//            else if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER)
//            {
//                float x = event.values[SensorManager.DATA_X];
//                float y = event.values[SensorManager.DATA_Y];
//                float z = event.values[SensorManager.DATA_Z];
//                accelerometerView.setText("Accelerometer: " + x + ", " + y + ", " + z);
//            }

        }
        //重写变化
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy)
        {
        }
    }

    //暂停传感器的捕获
    @Override
    protected void onPause()
    {
        sensorManager.unregisterListener(sensorEventListener);
        super.onPause();
    }

    class BackListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            MapActivity.this.finish();
        }
    }

    //通过与最近三个信号的距离与坐标,计算出当前位置;
    //前提是三个信号位置是固定的
    public Map getLocation(ZigBee zb1, ZigBee zb2, ZigBee zb3){
        Map location = new HashMap();
        //三个方程式算出坐标;

        return location;
    }

}
